//
//  OAAddressViewController.m
//  OASystem
//
//  Created by zhoupengfei on 16/1/17.
//  Copyright © 2016年 zpf. All rights reserved.
//

#import "OAAddressViewController.h"
#import "HttpTools.h"
#import "OADepAddressController.h"
#import "MBProgressHUD.h"
@interface AddressCellList : UITableViewCell

@property (weak, nonatomic) IBOutlet UIImageView *imageVIew;
@property (weak, nonatomic) IBOutlet UILabel *subject;


@end

@implementation AddressCellList



@end

@interface OAAddressViewController ()<UIAlertViewDelegate>
@property(nonatomic,strong)NSMutableArray* contents;
@property(strong,nonatomic)NSMutableArray * searchResult;
@end

@implementation OAAddressViewController

-(NSMutableArray*)contents{
    if (_contents == nil) {
        _contents = [NSMutableArray array];
    }
    
    return _contents;
}

-(NSMutableArray*)searchResult{
    if (_searchResult == nil) {
        _searchResult = [NSMutableArray array];
    }
    
    return _searchResult;
}


- (void)viewDidLoad {
// if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
//        self.automaticallyAdjustsScrollViewInsets = NO;
//}
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
 self.navigationController.navigationBar.barTintColor = [UIColor blueColor];
 self.navigationController.navigationBar.tintColor = [UIColor whiteColor];    
  NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName, nil];
[self.navigationController.navigationBar setTitleTextAttributes:attributes];

self.title = @"桐乡通讯录";
    [super viewDidLoad];
    [self requestData];
    [self setRightItem];
}

-(void)requestData{
    [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray *  array = @[@{@"userId":userId}];;
    [HttpTools requestWithURL:KExchother arrays:array method:@"globalTxlTree" success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        NSLog(@"%@",JSON);
        NSArray * tempArray = JSON[@"data"];
        NSDictionary *dict = [tempArray firstObject];
        self.contents = dict[@"Parent"];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
    }];
}

-(void)setRightItem{
    UIBarButtonItem * rightitem = [[UIBarButtonItem alloc] initWithTitle:@"搜索" style:UIBarButtonItemStylePlain target:self action:@selector(searchAction)];
    self.navigationItem.rightBarButtonItem = rightitem;
}

-(void)searchAction{

    UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"搜索通讯录" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alertView.delegate = self;
    alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField * textField = [alertView textFieldAtIndex:0];
    textField.placeholder = @"请输入关键字";
    [alertView show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

    if (buttonIndex == 1) {
        UITextField * textField = [alertView textFieldAtIndex:0];
        if (textField.text.length == 0) {
            return;
        }
        [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
        NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
        NSArray *  array = @[@{@"condition":textField.text},@{@"userId":userId},@{@"page":@"1"}];;
        [HttpTools requestWithURL:KExchother arrays:array method:@"adressListSearch" success:^(id JSON) {
       
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
            NSArray * tempArray = JSON[@"data"];
            
            if (tempArray.count) {
    

            NSDictionary *dict = [tempArray firstObject];
           [self.searchResult removeAllObjects];
            if (dict) {
//                [self.searchResult addObject:dict];
                self.searchResult = [tempArray mutableCopy];
            }
          if (JSON) {
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            OADepAddressController * depVC = (OADepAddressController*)[storyBoard instantiateViewControllerWithIdentifier:@"DepAddressID" ];
            depVC.contents = self.searchResult;
            depVC.hidesBottomBarWhenPushed = YES;

            [self.navigationController pushViewController:depVC animated:YES];
}
            
}else{
NSLog(@"11111");
}
            
        } failure:^(NSError *error) {
            [MBProgressHUD hideAllHUDsForView:self.navigationController.view animated:YES];
        }];
    }
    
    
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AddressCellList * cell = [tableView dequeueReusableCellWithIdentifier:@"addressCellList" forIndexPath:indexPath];
    NSDictionary * dict = self.contents[indexPath.row];
    cell.subject.text = dict[@"bmmc"];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contents.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    OADepAddressController * depVC = (OADepAddressController*)[storyBoard instantiateViewControllerWithIdentifier:@"DepAddressID" ];
     NSDictionary * dict = self.contents[indexPath.row];
    depVC.path = dict[@"path"];
                depVC.hidesBottomBarWhenPushed = YES;

    [self.navigationController pushViewController:depVC animated:YES];
}

@end
